package edu.westga.cs3152.wordmojo;

import java.awt.*;
import java.util.ArrayList;

import javax.swing.*;

public class GamePanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private GameBoard board;
	private Controller gameController;
	private int cellHeight = 50, cellWidth = 50;
	private GUIObserver observer;
	private ArrayList<String> suggestedWords;;
	
	private TransparentButton submitButton;
	private TransparentButton suggestWordsButton;
	private JLabel selectedLetterLabel; 
	private JLabel suggestedWordsLabel;
	private JLabel wordOneLabel;
	private JLabel wordTwoLabel;
	private JLabel turnLabel;
	private JLabel oneUseLabel;
	private JLabel gameOverLabel; 

	public GamePanel(GameBoard board) {
		this.setPreferredSize(new Dimension(600, 600));
		this.board = board;
		this.gameController = new Controller(board, this);
		this.setLayout(null);
		this.observer = new GUIObserver(gameController);
		this.setBoardComponentProperties();
		this.suggestedWords = board.getSuggestedWords();
		this.setFocusable(true);
		this.setBackground(new Color(103, 12, 21));
	}

	private void setBoardComponentProperties() {
		
		suggestedWords = board.getSuggestedWords();
		
		for (int row = 0; row < board.getBOARD_ROWS(); row++) {
			for (int column = 0; column < board.getBOARD_COLUMNS(); column++) {
				Letter currentLetter = board.getPiece(row, column);
				currentLetter.setBounds(column * 50, row * 50, 50, 50);
				currentLetter.setOpaque(false);
				currentLetter.addActionListener(observer);
			}
		}

		for (int i = 0; i < 7; i++) {
			Letter[] playerLetters = board.getPlayerLetters();
			Letter currentLetter = playerLetters[i];
			currentLetter.setOnBoard(false);
			currentLetter.setBounds(50 + 50 * i, 500, 50, 50);
			currentLetter.setOpaque(false);
			currentLetter.addActionListener(observer);
		}

		submitButton = new TransparentButton();
		submitButton.setBounds(50 + 50 * 8 + 25, 500, 100, 50);
		submitButton.setOpaque(false);
		submitButton.setName("submit");
		submitButton.addActionListener(observer);
		this.add(submitButton);
		
		suggestWordsButton = new TransparentButton();
		suggestWordsButton.setBounds(50 + 50 * 8 + 25, 300, 100, 50);
		suggestWordsButton.setOpaque(false);
		suggestWordsButton.setName("help");
		suggestWordsButton.addActionListener(observer);
		this.add(suggestWordsButton);
		
		selectedLetterLabel = new JLabel();
		selectedLetterLabel.setText("Selected Letter");
		selectedLetterLabel.setForeground(Color.WHITE);
		selectedLetterLabel.setBounds(50 + 50 * 8 + 30, 360, 100, 50);
		this.add(selectedLetterLabel);
		
		suggestedWordsLabel = new JLabel();
		suggestedWordsLabel.setText("Possible Words:" );
		suggestedWordsLabel.setForeground(Color.WHITE);
		suggestedWordsLabel.setBounds(50 + 50 * 8 + 30, 130, 200, 200);
		this.add(suggestedWordsLabel);
		
		wordOneLabel = new JLabel();
		wordOneLabel.setText(suggestedWords.get(0));
		wordOneLabel.setForeground(Color.WHITE);
		wordOneLabel.setBounds(50 + 50 * 8 + 30, 145, 200, 200);
		this.add(wordOneLabel);
		
		wordTwoLabel = new JLabel();
		wordTwoLabel.setText(suggestedWords.get(1));
		wordTwoLabel.setForeground(Color.WHITE);
		wordTwoLabel.setBounds(50 + 50 * 8 + 30, 160, 200, 200);
		this.add(wordTwoLabel);
		
		turnLabel = new JLabel();
		turnLabel.setText("Turn " + board.getTurn() + " of 8");
		turnLabel.setForeground(Color.WHITE);
		turnLabel.setBounds(50 + 50 * 8 + 30, 450, 100, 50);
		this.add(turnLabel);
		
		oneUseLabel = new JLabel();
		oneUseLabel.setText("(One use only!)");
		oneUseLabel.setForeground(Color.WHITE);
		oneUseLabel.setBounds(50 + 50 * 8 + 30, 335, 100, 50);
		this.add(oneUseLabel);
		
		gameOverLabel = new JLabel();
		gameOverLabel.setText("GAME OVER");
		gameOverLabel.setForeground(Color.WHITE);
		gameOverLabel.setBounds(50 + 50 * 8 + 30, 100, 100, 50);
		this.add(gameOverLabel);
	}

	public void refresh() {
		repaint();
	}

	public void paintComponent(Graphics g) {
		
		this.removeAll();
		super.paintComponent(g);

		
		this.suggestedWords = board.getSuggestedWords();
		// Get the entire board and paint it
		for (int row = 0; row < board.getBOARD_ROWS(); row++) {
			for (int column = 0; column < board.getBOARD_COLUMNS(); column++) {
				Letter currentLetter = board.getPiece(row, column);
				this.add(currentLetter);
				char current = currentLetter.getLetter();
				Image letterImage = Toolkit.getDefaultToolkit().getImage("Images/" + current + ".png");
				g.drawImage(letterImage, column * cellWidth, row * cellHeight,cellWidth, cellHeight, this);
			}
		}
		for (int i = 0; i < 7; i++) {
			Letter[] playerLetters = board.getPlayerLetters();
			Letter currentLetter = playerLetters[i];
				
			currentLetter.setOnBoard(false);
			currentLetter.setBounds(50 + 50 * i, 500, 50, 50);
			currentLetter.setOpaque(false);
			currentLetter.addActionListener(observer);
				
			this.add(currentLetter);
			char current = currentLetter.getLetter();
			Image letterImage = Toolkit.getDefaultToolkit().getImage("Images/" + current + ".png");
			g.drawImage(letterImage, (i + 1) * cellWidth, 500, cellWidth,cellHeight, this);
		}

		this.add(submitButton);
		this.add(selectedLetterLabel);
		this.add(suggestWordsButton);
		this.add(turnLabel);
		this.add(oneUseLabel);
		
		
		wordOneLabel.setText(suggestedWords.get(0));
		wordTwoLabel.setText(suggestedWords.get(1));
		turnLabel.setText("Turn " + board.getTurn() + " of 8");
		
		if(board.showHint == true){
			this.add(suggestedWordsLabel);
			this.add(wordOneLabel);
			this.add(wordTwoLabel);
		}
		if(board.getTurn() == 8){
			this.add(gameOverLabel);
		}
		
		
		Image submitImage = Toolkit.getDefaultToolkit().getImage("Images/submit.png");
		g.drawImage(submitImage, 9 * cellWidth + 25, 500, cellWidth * 2 + 1,cellHeight, this);
		
		Image helpImage = Toolkit.getDefaultToolkit().getImage("Images/help.png");
		g.drawImage(helpImage, 50 + 50 * 8 + 25, 300, 100, 50, this);

		Letter selectedLetter = board.getSelectedTile();
		Image selectedLetterImage = Toolkit.getDefaultToolkit().getImage("Images/"+ selectedLetter.getLetter() +".png");
		g.drawImage(selectedLetterImage, 10 * cellWidth, 400, cellWidth ,cellHeight, this);
	}
}
